Skip to content

rust testcontainer framework #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 20, 2025
Merged

rust testcontainer framework #41

merged 12 commits into from
Feb 20, 2025

Conversation

kpwebb
Copy link
Contributor

@kpwebb kpwebb commented Feb 10, 2025

Here's my initial shot at a testcontainers implementation for the Rust SDK.

I've got the core functionality working, but wanted to share while in progress to see if adding a new "restate-test-utils" crate to the SDK is ok. Also wanted to solicit feedback on ways to extend the test implementation (e.g. mirroring Temporal's approach), per discussions on Discord.

Here's the current test implementation for a service as defined in tests/test_container.rs

#[tokio::test]
async fn test_container_image() {

    let test_container = TestContainer::new().await.unwrap();

    // uses higher port number to avoid collisions
    // with non-test instances running locally
    let host_port:u16 = 19080;
    let host_address = format!("0.0.0.0:{}", host_port);

    println!("starting host");
    // boot restate server
    tokio::spawn(async move {
        HttpServer::new(
            Endpoint::builder()
                .bind(MyServiceImpl.serve())
                .build(),
        ).listen_and_serve(host_address.parse().unwrap()).await;
    });

    use restate_sdk::service::Discoverable;
    let my_service:Service = ServeMyService::<MyServiceImpl>::discover();

    let registered = test_container.register(host_port).await;
    assert!(registered.is_ok());

    TestContainer::delay(1000).await;

    let response = test_container.invoke(my_service, "my_handler".to_string()).await;

}

@slinkydeveloper
Copy link
Collaborator

This is awesome, i'll give a look in the next days!

Copy link
Collaborator

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just left few initial comments, thanks for the contribution!

@kpwebb
Copy link
Contributor Author

kpwebb commented Feb 11, 2025

@slinkydeveloper thanks for the feedback! Just pushed an update that captures the above. Here's the new test implementation:

#[tokio::test]
async fn test_container_image() {

    let mut test_container = TestContainer::new("docker.io/restatedev/restate", "latest").await.unwrap();

    let endpoint = Endpoint::builder()
                    .bind(MyServiceImpl.serve())
                    .build();

    test_container.serve_endpoint(endpoint).await;

    // optionally insert a delays via tokio sleep
    TestContainer::delay(1000).await;

    // optionally call invoke on service handlers
    use restate_sdk::service::Discoverable;
    let my_service:Service = ServeMyService::<MyServiceImpl>::discover();
    let invoke_response = test_container.invoke(my_service, "my_handler").await;

    assert!(invoke_response.is_ok());

    println!("invoke response:");
    println!("{}", invoke_response.unwrap().text().await.unwrap());

}

Copy link
Collaborator

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks again for the contribution, I had some time to take a look, it's getting there, I think the API needs another pass

Copy link
Collaborator

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome. Run just check locally to fix linting/formatting issues and then we should be good to go!

@kpwebb
Copy link
Contributor Author

kpwebb commented Feb 19, 2025

thanks again for the recs and sorry for not running lint/fmt! should be good to go now

Copy link
Collaborator

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor nit, and I merge

@slinkydeveloper slinkydeveloper merged commit a656187 into restatedev:main Feb 20, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants